/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUITerritorio.java
 *
 * Created on 5/12/2010, 02:42:11 PM
 */

package snakesandladders;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author andres
 */
public class GUITerritorio extends javax.swing.JFrame {

    
    Matriz matriz;
    JLabel matrizLabels[][];
    int matrizJuego[][],tamanoTablero,jugadorInicia,icabezaJug,jcabezaJug,icabezaMaq,jcabezaMaq,tocaTurno;
    int jugadorGanador;

    /** Creates new form GUITerritorio */
    public GUITerritorio(int jugadorInicia)
    {
        this.jugadorInicia=jugadorInicia;
        jugadorGanador=-1;
        this.tocaTurno=jugadorInicia;
        this.tamanoTablero=5;
        initComponents();   
        matrizLabels=new JLabel[tamanoTablero][tamanoTablero];
        matrizJuego=new int[tamanoTablero][tamanoTablero];
        matrizJuego[0][0]=1;
        matrizJuego[4][4]=2;
        matriz=new Matriz(matrizJuego);
        
        icabezaJug=jcabezaJug=0;
        icabezaMaq=jcabezaMaq=4;

        matriz.setiJugador1(0);
        matriz.setjJugador1(0);
        matriz.setiMaquina(4);
        matriz.setjMaquina(4);

        
        crearMatrizLabels();
        jlNumCasillasJ.setText("1");
        jlNumCasillasM.setText("1");

        setVisible(true);
        pack();
        System.out.println(jugadorInicia);
        if(jugadorInicia==1)
        {
            juegaJugador();
        }
        else{juegaMaquina();}
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        casilla6 = new javax.swing.JLabel();
        casilla2 = new javax.swing.JLabel();
        casilla3 = new javax.swing.JLabel();
        casilla5 = new javax.swing.JLabel();
        casilla4 = new javax.swing.JLabel();
        casilla1 = new javax.swing.JLabel();
        casilla7 = new javax.swing.JLabel();
        casilla8 = new javax.swing.JLabel();
        casilla9 = new javax.swing.JLabel();
        casilla10 = new javax.swing.JLabel();
        casilla11 = new javax.swing.JLabel();
        casilla12 = new javax.swing.JLabel();
        casilla13 = new javax.swing.JLabel();
        casilla14 = new javax.swing.JLabel();
        casilla15 = new javax.swing.JLabel();
        casilla16 = new javax.swing.JLabel();
        casilla17 = new javax.swing.JLabel();
        casilla19 = new javax.swing.JLabel();
        casilla20 = new javax.swing.JLabel();
        casilla18 = new javax.swing.JLabel();
        casilla21 = new javax.swing.JLabel();
        casilla22 = new javax.swing.JLabel();
        casilla23 = new javax.swing.JLabel();
        casilla24 = new javax.swing.JLabel();
        casilla25 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jlDuenoTurno = new javax.swing.JLabel();
        jlNumCasillasM = new javax.swing.JLabel();
        jlNumCasillasJ = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel1.setText("Minijuego Territorio");

        jPanel1.setBorder(javax.swing.BorderFactory.createCompoundBorder());

        casilla6.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla6.setOpaque(true);
        casilla6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla2.setOpaque(true);
        casilla2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla3.setOpaque(true);
        casilla3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla5.setOpaque(true);
        casilla5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla4.setOpaque(true);
        casilla4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla1.setBackground(new java.awt.Color(207, 22, 40));
        casilla1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla1.setOpaque(true);

        casilla7.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla7.setOpaque(true);
        casilla7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla8.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla8.setOpaque(true);
        casilla8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla9.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla9.setOpaque(true);
        casilla9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla10.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla10.setOpaque(true);
        casilla10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla11.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla11.setOpaque(true);
        casilla11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla12.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla12.setOpaque(true);
        casilla12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla13.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla13.setOpaque(true);
        casilla13.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla14.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla14.setOpaque(true);
        casilla14.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla15.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla15.setOpaque(true);
        casilla15.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla16.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla16.setOpaque(true);
        casilla16.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla17.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla17.setOpaque(true);
        casilla17.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla19.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla19.setOpaque(true);
        casilla19.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla20.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla20.setOpaque(true);
        casilla20.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla18.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla18.setOpaque(true);
        casilla18.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla21.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla21.setOpaque(true);
        casilla21.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla22.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla22.setOpaque(true);
        casilla22.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla23.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla23.setOpaque(true);
        casilla23.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla24.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla24.setOpaque(true);
        casilla24.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                realizarJugada(evt);
            }
        });

        casilla25.setBackground(new java.awt.Color(31, 104, 34));
        casilla25.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        casilla25.setOpaque(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(casilla21, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla22, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla23, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla24, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla25, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(casilla16, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla17, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla18, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla19, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla20, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(casilla11, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla12, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla13, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla14, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla15, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(casilla1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(casilla6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla10, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(casilla1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(casilla7, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(casilla8, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(casilla9, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(casilla10, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(casilla6, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(casilla11, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla12, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla13, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla14, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(casilla15, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(casilla17, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casilla18, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casilla19, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casilla20, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(casilla16, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(casilla22, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casilla23, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casilla24, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(casilla25, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(casilla21, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel22.setText("Turno de:");

        jLabel23.setText("Numero Casillas Maquina:");

        jLabel24.setText("Numero Casillas Jugador:");

        jLabel25.setText("Jugador:");

        jLabel26.setText("Maquina: ");

        jlNumCasillasM.setText("jLabel28");

        jlNumCasillasJ.setText("jLabel28");

        jLabel30.setBackground(new java.awt.Color(207, 22, 40));
        jLabel30.setOpaque(true);

        jLabel31.setBackground(new java.awt.Color(31, 104, 34));
        jLabel31.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(253, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel24)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlNumCasillasJ, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel22)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jlDuenoTurno, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel23)
                        .addGap(12, 12, 12)
                        .addComponent(jlNumCasillasM, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel25)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel30, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel26)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel31, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(38, 38, 38))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(jlDuenoTurno))
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel25)
                    .addComponent(jLabel30, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel31, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel26))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel23)
                    .addComponent(jlNumCasillasM, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel24)
                    .addComponent(jlNumCasillasJ, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(107, 107, 107))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void realizarJugada(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_realizarJugada
        // TODO add your handling code here:
         JLabel label=(JLabel)evt.getSource();

         int pos[];
        pos=buscarPosLabel(label);

        if(!verificarSeleccion(label,pos))
        {
            return;
        }

            matrizJuego[pos[0]][pos[1]]=1;
            matrizLabels[pos[0]][pos[1]].setBackground(new java.awt.Color(207, 22, 40));
            icabezaJug=pos[0];
            jcabezaJug=pos[1];
            matriz.setNumCeldasJugador1(1);
            jlNumCasillasJ.setText(matriz.getNumCeldasJugador1()+"");
            tocaTurno=2;
            matriz.setiJugador1(icabezaJug);
            matriz.setjJugador1(jcabezaJug);
            if(verificarFinJuego())
            {
                 try
                {
                    Thread.sleep (3000);
                } catch (Exception e){}
                this.dispose();
                return;
            }
            jlDuenoTurno.setText("Maquina");
            juegaMaquina();


    }//GEN-LAST:event_realizarJugada

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel casilla1;
    private javax.swing.JLabel casilla10;
    private javax.swing.JLabel casilla11;
    private javax.swing.JLabel casilla12;
    private javax.swing.JLabel casilla13;
    private javax.swing.JLabel casilla14;
    private javax.swing.JLabel casilla15;
    private javax.swing.JLabel casilla16;
    private javax.swing.JLabel casilla17;
    private javax.swing.JLabel casilla18;
    private javax.swing.JLabel casilla19;
    private javax.swing.JLabel casilla2;
    private javax.swing.JLabel casilla20;
    private javax.swing.JLabel casilla21;
    private javax.swing.JLabel casilla22;
    private javax.swing.JLabel casilla23;
    private javax.swing.JLabel casilla24;
    private javax.swing.JLabel casilla25;
    private javax.swing.JLabel casilla3;
    private javax.swing.JLabel casilla4;
    private javax.swing.JLabel casilla5;
    private javax.swing.JLabel casilla6;
    private javax.swing.JLabel casilla7;
    private javax.swing.JLabel casilla8;
    private javax.swing.JLabel casilla9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel jlDuenoTurno;
    private javax.swing.JLabel jlNumCasillasJ;
    private javax.swing.JLabel jlNumCasillasM;
    // End of variables declaration//GEN-END:variables


    private void crearMatrizLabels()
    {
        matrizLabels[0][0]=casilla1;
        matrizLabels[0][1]=casilla2;
        matrizLabels[0][2]=casilla3;
        matrizLabels[0][3]=casilla4;
        matrizLabels[0][4]=casilla5;
        matrizLabels[1][0]=casilla6;
        matrizLabels[1][1]=casilla7;
        matrizLabels[1][2]=casilla8;
        matrizLabels[1][3]=casilla9;
        matrizLabels[1][4]=casilla10;
        matrizLabels[2][0]=casilla11;
        matrizLabels[2][1]=casilla12;
        matrizLabels[2][2]=casilla13;
        matrizLabels[2][3]=casilla14;
        matrizLabels[2][4]=casilla15;
        matrizLabels[3][0]=casilla16;
        matrizLabels[3][1]=casilla17;
        matrizLabels[3][2]=casilla18;
        matrizLabels[3][3]=casilla19;
        matrizLabels[3][4]=casilla20;
        matrizLabels[4][0]=casilla21;
        matrizLabels[4][1]=casilla22;
        matrizLabels[4][2]=casilla23;
        matrizLabels[4][3]=casilla24;
        matrizLabels[4][4]=casilla25;
    }

    public int[] buscarPosLabel(JLabel label)
    {
        int[] posIJLabel=new int[2];

        for(int i=0; i<tamanoTablero ; i++)
        {
            for(int j=0; j<tamanoTablero ; j++)
            {
                if(matrizLabels[i][j].equals(label))
                {
                    posIJLabel[0]=i;
                    posIJLabel[1]=j;
                }
            }
        }

        return posIJLabel;
    }


    public int[][] copiarMatriz(int matrizACopiar[][])
    {
        int [][] matriz;
        matriz=new int[5][5];
        int valor=0;

        for(int i=0; i<tamanoTablero ; i++)
        {
            for(int j=0; j<tamanoTablero; j++)
            {
                valor=matrizACopiar[i][j];
                matriz[i][j]=valor;

            }
        }

        return matriz;
    }

    private void juegaMaquina()
    {

        //objTerritorio=new Territorio();
        int desicionI,desicionJ;
        desicionI=desicionJ=0;
        Matriz m;
        m=matriz;
       /* try {
            m = matriz.clonar();
            try
        {
            Thread.sleep (3000);
        } catch (Exception e)
            {
            // Mensaje en caso de que falle
            }*/
        

            System.out.println("terrotorio inicio");
        Territorio objTerritorio=new Territorio(m);
          desicionI=objTerritorio.getDesicionI();
        desicionJ=objTerritorio.getDesicionJ();
       
        System.out.println("terrotorio fin");             

         System.out.println("jugada obtenida "+desicionI+" "+desicionJ);
         matrizJuego[desicionI][desicionJ]=1;
         matrizLabels[desicionI][desicionJ].setBackground(new java.awt.Color(31, 104, 34));
         icabezaMaq=desicionI;
         jcabezaMaq=desicionJ;

         matriz.setiMaquina(icabezaMaq);
         matriz.setjMaquina(jcabezaMaq);
         matriz.setNumCeldasMaquina(1);
         jlNumCasillasM.setText(matriz.getNumCeldasMaquina()+"");
        if(verificarFinJuego())
        {
             try
                {
                    Thread.sleep (3000);
                } catch (Exception e){}
            this.dispose();
            return;
        }
        tocaTurno=1;
        juegaJugador();
    }

    private void juegaJugador()
    {

        jlDuenoTurno.setText("Jugador");
        

    }

    public boolean verificarFinJuego()
    {
        int celdasM,celdasJ,celdasLibresM,celdasLibresJ;
        celdasJ=celdasLibresJ=celdasLibresM=celdasM=0;

        celdasJ=matriz.getNumCeldasJugador1();
        celdasM=matriz.getNumCeldasMaquina();
        int total=celdasJ;
        total+=celdasM;

        if(total==25)
        {
             if(celdasM<celdasJ)
             {
                 jugadorGanador=1;
                 JOptionPane.showMessageDialog(null,  "Felicitaciones!! Has ganado la partida.","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
             }
             else
             {
                JOptionPane.showMessageDialog(null,  "Has perdido.","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
                jugadorGanador=2;
             }if(jugadorGanador!=-1)
           System.out.println("GAno "+jugadorGanador);
            return true;
        }

         for(int i=icabezaJug-1; i<icabezaJug+2 ; i++)
        {
            for(int j=jcabezaJug-1; j<jcabezaJug+2; j++)
            {
                if(i>=0 && i<tamanoTablero && j>=0 && j<tamanoTablero)
                {
                    if(matriz.getValorPosicion(i, j)==0)
                    {
                        celdasLibresJ++;
                    }
                }
            }
        }
        for(int i=icabezaMaq-1; i<icabezaMaq+2 ; i++)
        {
            for(int j=jcabezaMaq-1; j<jcabezaMaq+2; j++)
            {
                if(i>=0 && i<tamanoTablero && j>=0 && j<tamanoTablero)
                {
                    if(matriz.getValorPosicion(i, j)==0)
                    {
                        celdasLibresM++;
                    }
                }
            }
        }

        if(celdasLibresJ==0 && celdasLibresM!=0)
        {
             JOptionPane.showMessageDialog(null,  "Has perdido. Quedaste atrapado","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
             jugadorGanador=2;if(jugadorGanador!=-1)
           System.out.println("GAno "+jugadorGanador);
            return true;
        }
        if( celdasLibresM==0 && celdasLibresJ!=0)
        {
            JOptionPane.showMessageDialog(null,  "Felicitaciones!! Has ganado la partida.","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
            jugadorGanador=1;if(jugadorGanador!=-1)
           System.out.println("GAno "+jugadorGanador);
            return true;
        }
        if(celdasLibresJ==0 && celdasLibresM==0)
        {
            if(celdasM<celdasJ)
             {
                 JOptionPane.showMessageDialog(null,  "Felicitaciones!! Has ganado la partida.","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
                 jugadorGanador=1;if(jugadorGanador!=-1)
           System.out.println("GAno "+jugadorGanador);
             }
             else if(celdasM==celdasJ)
             {
                JOptionPane.showMessageDialog(null,  "El juego quedo en empate.","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
                jugadorGanador=0;if(jugadorGanador!=-1)
           System.out.println("GAno "+jugadorGanador);
             }
             else
             {
                JOptionPane.showMessageDialog(null,  "Has perdido. Tienes menos casillas","Fin del Juego", JOptionPane.INFORMATION_MESSAGE);
                jugadorGanador=2;if(jugadorGanador!=-1)
           System.out.println("GAno "+jugadorGanador);
             }
            return true;
        }

       
        
        return false;
    }

    public boolean verificarPosicionLabel(int[] pos)
    {
        if(icabezaJug+1<pos[0] || icabezaJug-1>pos[0] || jcabezaJug-1>pos[1] || jcabezaJug+1<pos[1])
        {
            return false;
        }
        
        return true;
    }
    
    public boolean verificarSeleccion(JLabel label,int[] pos)
    {
        if(label.getBackground().equals(new java.awt.Color(207, 22, 40)) || label.getBackground().equals(new java.awt.Color(31, 104, 34)))
        {
            JOptionPane.showMessageDialog(null, "La casilla esta ocupada.","Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(tocaTurno!=1)
        {
            JOptionPane.showMessageDialog(null,  "Todavia no es su turno, espere un momento y vuelva a intentarlo.","Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if( !verificarPosicionLabel(pos))
        {
            JOptionPane.showMessageDialog(null,  "Debe seleccionar una casilla adyacente a la casilla ("+icabezaJug+","+jcabezaJug+")","Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;
    }

    public int getJugadorGanador() {
        return jugadorGanador;
    }

    

}
